package com.ms.service.service;

import com.ms.service.dao.ShoppingCartDao;
import com.ms.service.entity.ShoppingCartRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by qihao on 2018/3/29.
 */
@Service
public class ShoppingCartService {

    @Autowired
    private  ShoppingCartDao shoppingCartDao;

    public boolean removeAllProductsfromSC(ShoppingCartRecord scr){

        int delRec = shoppingCartDao.deleteRecord(scr);
        if(delRec == 1){
            return true;
        }else{
            return false;
        }
    }
    public boolean removeOneProductfromSC(ShoppingCartRecord scr){
        scr=shoppingCartDao.getRecord(scr);
        if(scr == null){
            return false;
        }
        int count = scr.getTotal();
        count --;
        if(count == 0){
            int influ = shoppingCartDao.deleteRecord(scr);
            if(influ == 1){
                return true;
            }else{
                return false;
            }

        }else{
            scr.setTotal(count);
            int influ = shoppingCartDao.updateRecord(scr);
            if(influ == 1){
                return true;
            }else{
                return false;
            }
        }
    }
    public boolean addProductToSC(ShoppingCartRecord scr){
        ShoppingCartRecord gscr=shoppingCartDao.getRecord(scr);
        if(gscr == null){
            gscr = new ShoppingCartRecord();
            gscr.setUserId(scr.getUserId());
            gscr.setProductId(scr.getProductId());
            gscr.setTotal(1);
            int influ = shoppingCartDao.addRecord(gscr);
            if(influ == 1){
                return true;
            }else{
                return false;
            }
        }else{
            gscr.setTotal(gscr.getTotal()+1);
            int influ = shoppingCartDao.updateRecord(gscr);
            if(influ == 1){
                return true;
            }else{
                return false;
            }
        }
    }
    public List<ShoppingCartRecord> initShoppinCart(Integer userId){
        return shoppingCartDao.initShoppinCart(userId);
    };
}
